package com.cmcc.linlaker.ui.upload.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cmcc.linlaker.R;
import com.cmcc.linlaker.ui.upload.bean.UploadQuestionBean;

import java.util.List;

public class UploadQuestionAdapter extends RecyclerView.Adapter<UploadQuestionAdapter.ViewHolder> {

    private List<UploadQuestionBean.UploadQuestionData> itemList;
    private Context mContext;

    public UploadQuestionAdapter(Context mContext, List<UploadQuestionBean.UploadQuestionData> itemList) {
        this.itemList = itemList;
        this.mContext = mContext;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.upload_question_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        UploadQuestionBean.UploadQuestionData item = itemList.get(position);

        holder.text_title.setText(item.title);
        holder.text_upload_date.setText("上报时间：" + item.createTime);
        holder.text_upload_address.setText("上报地点：" + item.reportPlace);

        if (item.deelFlag.equals("1")) {
            holder.text_is_self.setVisibility(View.VISIBLE);
        } else {
            holder.text_is_self.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(item.state)) {
            if (item.state.equals("1")) {
                //已处置
                holder.image_state.setBackgroundResource(R.drawable.button_all_round_bg_green);
                holder.text_state.setText("已处置");
                holder.text_state.setTextColor(0xff52C41A);
            } else if (item.state.equals("9")) {
                //处置中
                holder.image_state.setBackgroundResource(R.drawable.button_all_round_bg_yellow);
                holder.text_state.setText("处置中");
                holder.text_state.setTextColor(0xffFF951C);
            } else {
                //未处置
                holder.image_state.setBackgroundResource(R.drawable.button_all_round_bg_red);
                holder.text_state.setText("未处置");
                holder.text_state.setTextColor(0xffff0000);
            }
        } else {
            holder.image_state.setBackgroundResource(R.drawable.button_all_round_bg_red);
            holder.text_state.setText("未处置");
            holder.text_state.setTextColor(0xffff0000);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public TextView text_title, text_upload_address, text_upload_date;
        public ImageView image_state;
        public TextView text_state, text_is_self;


        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);

            text_title = itemView.findViewById(R.id.text_title);
            text_upload_address = itemView.findViewById(R.id.text_upload_address);
            text_upload_date = itemView.findViewById(R.id.text_upload_date);

            image_state = itemView.findViewById(R.id.image_state);
            text_state = itemView.findViewById(R.id.text_state);
            text_is_self = itemView.findViewById(R.id.text_is_self);
        }

        @Override
        public void onClick(View v) {
            onItemClickListener.onItemClick(getAdapterPosition());
        }
    }

}

